package com.xu.controller;

import com.xu.mapper.CartMapper;
import com.xu.mapper.ProductMapper;
import com.xu.mapper.Vouchers_userMapper;
import com.xu.pojo.Cart;
import com.xu.pojo.Product;
import com.xu.pojo.Vouchers;
import com.xu.pojo.Vouchers_user;
import com.xu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class Vouchers_userController {

    @Autowired
    private Vouchers_userMapper vouchers_userMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;


    @Autowired
    private ProductService productService;

    @RequestMapping("/vouchersGet/{vid}/{uid}")
    public String vouchersGet(@PathVariable("vid")int vid, @PathVariable("uid")int uid, Model model)
    {
        Vouchers_user vouchers = new Vouchers_user();
        vouchers.setVid(vid);
        vouchers.setUid(uid);
        Vouchers_user vouchers_user = vouchers_userMapper.queryVouchersByVidUid(vouchers);
        if (vouchers_user==null)
        {
            vouchers_userMapper.vouchersGet(vouchers);
            model.addAttribute("msg2", "已领取优惠券");

            //查询商品
            List<Product> products = productService.queryAllProduct();
            model.addAttribute("prducts", products);

            List<Product> products1 = productMapper.randomSelectProduct();
            model.addAttribute("products1", products1);

            //查询购物车
            List<Cart> carts = cartMapper.queryCartByUid(uid);
            model.addAttribute("carts", carts);

            return "index-2";
        }
        else
        {

            //查询商品
            List<Product> products = productService.queryAllProduct();
            model.addAttribute("prducts", products);

            List<Product> products1 = productMapper.randomSelectProduct();
            model.addAttribute("products1", products1);

            //查询购物车
            List<Cart> carts = cartMapper.queryCartByUid(uid);
            model.addAttribute("carts", carts);

            model.addAttribute("msg2", "无法重复领取优惠券");
            return "index-2";
        }
    }
}
